package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * 通用类
 */
@Slf4j
@RestController
@Api(tags = "通用接口")
public class CommonController {
    @Autowired
    private AliOssUtil aliOssUtil;
    /**
     * 文件上传
     * @return
     */
    @PostMapping("/admin/common/upload")
    @ApiOperation("文件上传")
    public Result<String> uplode(MultipartFile file){
        log.info("上传文件{}",file);
        try {
            String originalFilename = file.getOriginalFilename(); //获取原文件名
            String substring = originalFilename.substring(originalFilename.lastIndexOf("."));//获取源文件后缀

            String s = UUID.randomUUID().toString() + substring;
            String upload = aliOssUtil.upload(file.getBytes(), s);

            return  Result.success(upload);
        } catch (IOException e) {
            log.info(MessageConstant.UPLOAD_FAILED);
        }
        return null;
    }


}
